package com.zhuyiyi.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuyiyi.mmall.entity.User;
import com.zhuyiyi.mmall.entity.UserAddress;
import com.zhuyiyi.mmall.service.CartService;
import com.zhuyiyi.mmall.service.UserAddressService;
import com.zhuyiyi.mmall.service.impl.CartServiceImpl;
import com.zhuyiyi.mmall.service.impl.UserAddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 11
 * @since 2021-05-10
 */
@Controller
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    private UserAddressServiceImpl userAddressService;
    @Autowired
    private CartServiceImpl cartService;

    @GetMapping("/list")
    public ModelAndView list(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userAddressList");
        User user = (User)session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",user.getId());
        modelAndView.addObject("addressList",userAddressService.list(wrapper));
        return modelAndView;
    }
    @GetMapping("/remove/{id}")
    public String remove(@PathVariable ("id") Integer id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
    userAddressService.remove(wrapper);
    return "redirect:/userAddress/list";

    }

       @GetMapping("/updateAddress/{id}/{address}/{remark}/{code}")


    public String updateAddress(
               @PathVariable("id") String id,
               @PathVariable("address") String address,
               @PathVariable("remark") String remark,
               @PathVariable("code") String code,
               HttpSession session)
    {

          userAddressService.updateById(id,address,remark,code,session);
//        System.out.println(id);
//        System.out.println(remark);
        return "redirect:/userAddress/list";
    }

}

